package test3.controller.impl;

import test3.bean.Admini;
import test3.bean.Employee;
import test3.controller.PageController;
import test3.dao.AdminiDao;
import test3.dao.EmployeeDao;
import test3.dao.impl.AdminiDaoImpl;
import test3.dao.impl.EmployeeDaoImpl;
import test3.file.FileModel;
import test3.file.FileOpreation;
import test3.runexception.NumberIsNotPositiveException;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public class PageControllerImpl implements PageController {

    private EmployeeDao dao=null;

    private FileModel model;

    private AdminiDao adminiDao;

    public PageControllerImpl(){
        dao= EmployeeDaoImpl.createEmployeeDao();
        model=new FileModel();
        adminiDao=AdminiDaoImpl.createAdminiDaoImpl();
    }

    private static Integer pageNo=1;

    private static Integer pageSize=5;

    private static Integer maxPageNo;

    @Override
    public List<Employee> showEmployees(Integer pageNo, Integer pageSize) {

        if (pageNo<1||pageSize<1)
            throw new NumberIsNotPositiveException("这不是一个小数！");

        List<Employee> employees=dao.queryAllByList();
        int employeeSiez=employees.size();

        //设置页号和页数量为用户设置的格式
        setPageNo(pageNo);
        setPageSize(pageSize);
        //设置最大的页号
        if (employeeSiez % pageSize >0)
            setMaxPageNo(employeeSiez/pageSize + 1);
        else
            setMaxPageNo(employeeSiez/pageSize);

        //得到一个所有员工信息的流
        Stream<Employee> employeeStream=employees.stream();
        //选中页的起始索引
        long firstNum=(pageNo-1)*pageSize;

        //筛选用户选择的分页的数据
        List<Employee> es=employeeStream.
                skip(firstNum).
                limit(pageSize).
                collect(Collectors.toList());

        return es;
    }

    @Override
    public List<Employee> showEmployees(Integer pageNo){
       return showEmployees(pageNo,5);
    }

    @Override
    public List<Employee>showEmployees(){
        return showEmployees(pageNo,5);
    }

    @Override
    public List<Employee> showNextPage() {
        if (pageNo<maxPageNo) {
            setPageNo(pageNo + 1);
            return showEmployees(pageNo);
        }
        return null;
    }

    @Override
    public List<Employee> showPrePage() {
        if (pageNo>0) {
            setPageNo(pageNo - 1);
            return showEmployees(pageNo);
        }
        return null;
    }

    @Override
    public List<Employee> updateOrInsert(Employee e) {

        if (e==null)
            return null;

        int id=e.getId();

        System.out.println(id);

        dao.insertOrUpdate(e);

        return showEmployees();
    }

    @Override
    public List<Employee> delete(int id) {
        if (id <= 0)
            return null;

        Employee employee=dao.queryById(id);

        dao.delete(id);

        model.afterDeleteOrUpdate(employee, FileOpreation.DELETE);

        return showEmployees();
    }

    @Override
    public Employee queryById(Integer id) {
        return dao.queryById(id);
    }


    @Override
    public void exit() {
        Map<Integer,Employee>map1=dao.getMap();
        HashMap<String,String>map2=adminiDao.getAllAdminis();
        try {

            model.outputAllEmployee(map1);
            model.saveAdministrators(map2);

        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Override
    public boolean login(Admini admini) {

        return adminiDao.loginAdmini(admini);
    }

    @Override
    public void addAdmir(Admini admini) {
        adminiDao.addAdmini(admini);
    }


    private static void setPageNo(Integer pageNo) {
        PageControllerImpl.pageNo = pageNo;
    }

    private static void setPageSize(Integer pageSize) {
        PageControllerImpl.pageSize = pageSize;
    }

    private static void setMaxPageNo(Integer maxPageNo) {
        PageControllerImpl.maxPageNo = maxPageNo;
    }
}
